#include"../mqserver/mq_queue.hpp"
#include <gtest/gtest.h>

mq::MsgQueueManager::ptr mqmp;


class QueueTest : public testing::Environment {
    public:
        virtual void SetUp() override {
            mqmp = std::make_shared<mq::MsgQueueManager>("./data/meta.db");
        }
        virtual void TearDown() override {
            mqmp->clear();
            std::cout << "最后的清理！！\n";
        }
};

void ConvertUnorderedMapToProtobufMap(  
    const std::unordered_map<std::string, std::string>& src,  
    google::protobuf::Map<std::string, std::string>* dest) {  
    dest->clear();  // 清空目标映射（如果需要的话）  
    for (const auto& pair : src) {  
        (*dest)[pair.first] = pair.second;  
    }  
}
// TEST(queue_test,insert_test)
// {
//     std::unordered_map<std::string, std::string> unorderedMap = {{"k1","v1"}}; 
//     google::protobuf::Map<std::string, std::string> protobufMap;
  
//     // 转换unorderedMap到protobufMap  
//     ConvertUnorderedMapToProtobufMap(unorderedMap, &protobufMap);  
//     mqmp->declareQueue("queue1", true, false, false, protobufMap);
//     mqmp->declareQueue("queue2", true, false, false, protobufMap);
//     mqmp->declareQueue("queue3", true, false, false, protobufMap);
//     mqmp->declareQueue("queue4", true, false, false, protobufMap);
//     ASSERT_EQ(mqmp->size(), 4);
// }

TEST(queue_test, select_test) {
    ASSERT_EQ(mqmp->exists("queue1"), false);
    ASSERT_EQ(mqmp->exists("queue2"), false);
    ASSERT_EQ(mqmp->exists("queue3"), true);
    ASSERT_EQ(mqmp->exists("queue4"), true);
    mq::MsgQueue::ptr mqp = mqmp->selectQueue("queue2");
    ASSERT_NE(mqp.get(), nullptr);
    ASSERT_EQ(mqp->name, "queue2");
    ASSERT_EQ(mqp->durable, true);
    ASSERT_EQ(mqp->exclusive, false);
    ASSERT_EQ(mqp->auto_delete, false);
    ASSERT_EQ(mqp->getArgs(), std::string("k1=v1&"));
}

TEST(queue_test, remove_test) {
    mqmp->deleteQueue("queue3");
    ASSERT_EQ(mqmp->exists("queue3"), false);
}

int main(int argc,char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new QueueTest);
    RUN_ALL_TESTS();
    return 0;
}